<?php

namespace App\Controllers\Lessions;

use App\Models\Lession\Lession;
use App\Models\Tag\Tags;
use \App\Libs\Views\Json;
use App\Models\Lession\Scolumn;
use App\Models\Lession\Course;
use App\Libs\Helper\CommonHelper;
use App\Models\Lession\SearchHistory;
/**
    课程搜索；
 */
class LessionSearch extends \App\Libs\Base\BaseAction
{

    public function execute()
    {
        if(!$this->request->hasQuery('key')){
            return \App\Libs\Views\Json::view(1, '缺少关键词', null);
        }
        $word = $this->request->getQuery('key');
        $page = $this->request->getQuery('page','int',1);
        $num = $this->request->getQuery('num','int',10);
        $offset = ($page-1) * $num;
        if(empty(trim($word))){
            return \App\Libs\Views\Json::view(1, '请输入搜索关键字', null);
        }
        if(!$this->request->hasQuery('type')){
            return \App\Libs\Views\Json::view(1, '请选择搜索类型！', []);
        }else{
            //返回相应的类型数据;1:课程；2：专栏；
            $type = $this->request->getQuery('type');
            //todo::写入搜索结果；
            if(isset($this->session->user->id)){
                $allSearchHIstory = SearchHistory::findAll([
                    'conditions' =>'uid=?1',
                    'bind' =>[1=>$this->session->user->id],
                    'order'=>'ctime desc'
                ]);
                if(count($allSearchHIstory) > 10){
                    $lastHistory = $allSearchHIstory->getLast();
                    $lastHistory->delete();
                }
                $newHistory = new SearchHistory();
                $newHistory->uid = $this->session->user->id;
                $newHistory->history = $word;
                $newHistory->save();
            }
            switch ($type){
                case '1':
                    $allMatchCourseObj = Course::findAll([
                        'conditions' => 'title like "%'.$word.'%"',
                        'columns' => 'id,list_logo,title,little_intro',
                        'offset' => $offset,
                        'limit' => $num
                    ]);
                    if(!$allMatchCourseObj->valid()){
                        return \App\Libs\Views\Json::view(1, '没有更多结果了！', []);
                    }else{
                        foreach($allMatchCourseObj as $val){
                            $allMatchCourse[] = array_merge($val->toArray(),['type'=>1,'type_text'=>'课程']);
                        }
                        return \App\Libs\Views\Json::view(0, '成功', $allMatchCourse);
                    }

                    break;
                case '2':
                    $allMatchScolumnOfLessions = Lession::findAll([
                        'conditions' => 'title like "%'.$word.'%" and type=2',
                        'columns' => 'id,coustar_id,title,little_intro',
                    ]);
                    if($allMatchScolumnOfLessions->valid()){
                        $scolumnIDArray=[];
                        foreach($allMatchScolumnOfLessions as $key => $val){
                            $scolumnIDArray[] = $val->coustar_id;
                        }
                        //去寻找专栏；
                        $allSearchColumn = Scolumn::findAll([
                            'conditions' => 'id in ({dsf:array})',
                            'columns' =>'id,list_logo,title,little_intro',
                            'bind' => ['dsf'=>$scolumnIDArray]
                        ]);
                        foreach($allSearchColumn as $val){
                            $allSearchColumnArray[] = array_merge($val->toArray(['id','list_logo','title','little_intro']),['type'=>2,'type_text'=>'专栏']);
                        }
                        $transColumnArray = CommonHelper::setColToKey($allSearchColumnArray,'id');
                        foreach($allMatchScolumnOfLessions as $key => $val){
                            if(isset($transColumnArray[$val['coustar_id']])){
                                $transColumnArray[$val['coustar_id']]['lessions'][] = $val;
                            }else{

                            }
                        }
                        $finalData =array_values($transColumnArray);
                        $finalDataData = array_slice($finalData,$offset,$num);
                        if(empty($finalDataData)){
                            return \App\Libs\Views\Json::view(1, '没有更多结果了！', []);
                        }
                    }else{
                        $finalDataData =[];
                    }
                    return \App\Libs\Views\Json::view(0, '成功', $finalDataData);
                    break;
            }

        }







        //return \App\Libs\Views\Json::view(1, '缺少关键词', null);
    }
}
